package cn.ijero.pictureselector.delegate;

import static androidx.lifecycle.Lifecycle.State.DESTROYED;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.Lifecycle;
import androidx.recyclerview.widget.GridLayoutManager;

import java.util.ArrayList;
import java.util.List;

import cn.ijero.pictureselector.R;
import cn.ijero.pictureselector.callback.PictureSelectorCallback;
import cn.ijero.pictureselector.config.PictureSelectorConfig;
import cn.ijero.pictureselector.data.GroupLocalMedia;
import cn.ijero.pictureselector.data.LocalMedia;
import cn.ijero.pictureselector.databinding.ActivityPictureSelectorBinding;
import cn.ijero.pictureselector.executor.ExecutorManager;
import cn.ijero.pictureselector.loader.db.PictureSelectorLoader;
import cn.ijero.pictureselector.util.ConvertUtil;


public class PictureSelectorActivity extends AppCompatActivity {

    private ActivityPictureSelectorBinding binding;
    private PictureSelectorAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityPictureSelectorBinding.inflate(getLayoutInflater());

        setContentView(binding.getRoot());

        initView();
        initViewListener();
        initData();
    }

    private void initViewListener() {
        adapter.setOnItemCheckedChangeListener(new PictureSelectorAdapter.onItemCheckedChangedListener() {
            @Override
            public void onCheckChanged() {
                List<GroupLocalMedia> checked = adapter.getChecked();
                MenuItem item = binding.toolbar.getMenu().findItem(R.id.menu_done);
                item.setEnabled(!checked.isEmpty());
            }
        });
        binding.toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        binding.toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                if (item.getItemId() == R.id.menu_done) {
                    List<GroupLocalMedia> checked = adapter.getChecked();
                    ArrayList<LocalMedia> localMedias = new ArrayList<>();
                    for (GroupLocalMedia localMedia : checked) {
                        localMedias.add(localMedia.getLocalMedia());
                    }
                    PictureSelectorCallback callback = PictureSelectorConfig.callback;
                    if (callback!=null){
                        callback.onSelected(localMedias);
                    }
                    finish();
                }
                return true;
            }
        });
    }

    private void initView() {
        adapter = new PictureSelectorAdapter();
        binding.recyclerView.setAdapter(adapter);
        GridLayoutManager glm = (GridLayoutManager) binding.recyclerView.getLayoutManager();
        if (glm != null) {
            glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    GroupLocalMedia groupLocalMedia = adapter.getData().get(position);
                    if (groupLocalMedia.isHeader()) {
                        return 4;
                    }
                    return 1;
                }
            });
        }
    }

    private void initData() {
        ExecutorManager.get().post(new Runnable() {
            @Override
            public void run() {
                ArrayList<LocalMedia> localMedias = PictureSelectorLoader.load(PictureSelectorActivity.this);
                Lifecycle.State currentState = getLifecycle().getCurrentState();
                if (currentState == DESTROYED) {
                    return;
                }
                ArrayList<GroupLocalMedia> groupLocalMedias = ConvertUtil.groupLocalMedias(localMedias);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter.setData(groupLocalMedias);
                    }
                });
            }
        });

    }

}
